﻿using UnityEditor;
using UnityEngine;

namespace NpcFramework.Editor
{
    [CustomEditor(typeof(NpcWayPoint))]
    public class NpcWayPointEditor : UnityEditor.Editor
    {
        private UnityEditor.Editor cacheEditor;
        private NpcWayPointDataSO npcWayPointDataSO;
        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();
            NpcWayPoint npcWayPoint = target as NpcWayPoint;
            var npcWayPath = npcWayPoint.transform.parent.GetComponent<NpcWayPath>();
            //if (npcWayPath.WayPathType == WayPathType.Car)
            {
                if (npcWayPointDataSO == null)
                {
                    npcWayPointDataSO = ScriptableObject.CreateInstance<NpcWayPointDataSO>();
                    npcWayPointDataSO.npcWayPoint = npcWayPoint;
                    npcWayPointDataSO.LoadBTreeByData();
                }

                if (cacheEditor == null)
                {
                    cacheEditor = UnityEditor.Editor.CreateEditor(npcWayPointDataSO);
                }
                cacheEditor.OnInspectorGUI();
            }

            GUILayout.BeginHorizontal();
            if (GUILayout.Button("删除"))
            {
                npcWayPoint.WayPath.DeleteIndexPoint(npcWayPoint);
            }

            if (GUILayout.Button("追加"))
            {
                npcWayPoint.WayPath.InsertWayPoint(npcWayPoint.Index);
            }

            if (GUILayout.Button("添加分支"))
            {
                npcWayPoint.WayPath.InsertBranchPoint(npcWayPoint.Index);
            }
            GUILayout.EndHorizontal();
        }
    }
}
